package com.example.demo.test;/**
 * Created by kete on 2018-11-29.
 */

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 作者：kete
 * 创建时间：2018-11-29 10:21
 * 功能描述：
 * 版本：
 */
public class CryptAesUtil {

    private final static String KEY 		= "s5zWmtS@(qm*T--f";

    private final static String VECTOR 	= "s5zWmtS@(qm*T--f";

    public static String encrypt(String content)  {

        try {
            SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("UTF-8"), "AES");
            IvParameterSpec iv = new IvParameterSpec(VECTOR.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8"));
            //需要替换加密过程成出现的换行符
            String sign = new BASE64Encoder().encode(encrypted);
            sign = sign.replaceAll("\\r\\n", "");
            sign = sign.replaceAll("\\n", "");
            return sign;// 此处使用BASE64做转码。
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String decrypt(String content)  {

        String originalString = "";
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("UTF-8"), "AES");
            IvParameterSpec iv = new IvParameterSpec(VECTOR.getBytes());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            originalString = new String(original, "UTF-8");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return originalString;
    }

    public static void main(String[] args) {
        String sb = "32uiLZEgnnn/7ohebt19RfL3bZl9LGavOr1EQBc+26o9txOL8VYxUNoIOIk1slGt08xJ52LRAVmbTS01V6X38cMmXcABoDXw/Y9sFa7DYthky7lzja3u1lrcrPbPfMf9T1X/tuO98lCaySwSlOQphN4aiFJjGqEQaHB5+oo+BhQ=";

        String resp = CryptAesUtil.decrypt(sb);

        System.out.println(resp);


    }
}
